package com.sharainy.contoller;


import com.sharainy.pojo.Agreement;
import com.sharainy.pojo.Area;
import com.sharainy.pojo.Meter;
import com.sharainy.pojo.User;
import com.sharainy.service.AgreementService;
import com.sharainy.service.AreaService;
import com.sharainy.service.MeterService;
import com.sharainy.util.DateUtil;
import com.sharainy.util.RestResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sharainy.util.DateUtil.chineseNumber2Int;


@Controller
@RequestMapping("/meter")
public class AddMeterController {
    @Autowired
    private MeterService meterService;
    @Autowired
    private AgreementService agreementService;
    @Autowired
    private AreaService areaService;

    @RequestMapping(value = "/meterPage")
    public String index(){
        return "addMeter";
    }
    @RequestMapping("/getMeterList")
    @ResponseBody
    public RestResponse getMeterList(Integer page, Integer limit, String meterId, HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        JSONObject jsonMain = new JSONObject();
        Map<String, String> map = new HashMap<String, String>();
        Map<String, String> subentryMap = new HashMap<String, String>();
        Map<String, String> agreementMap = new HashMap<String, String>();
        if (meterId!=null&&!"".equals(meterId)){
            map.put("meterId",meterId);
        }
        System.out.println("======"+user);
        map.put("companyId",user.getCompanyid()+"");
        List<Meter> meterList = meterService.selectMeter(map);
        List<Agreement> subentryInfo = agreementService.getSubentryInfo();
        for (Agreement subentry :subentryInfo){
            subentryMap.put(subentry.getId()+"",subentry.getName());
        }
        List<Agreement> agreementType = agreementService.getAgreementType();
        for (Agreement agreement :agreementType){
            agreementMap.put(agreement.getId()+"",agreement.getName());
        }
        for (Meter meter :meterList){
            String[] pos = meter.getPosition().split("-");
            String area1Name = areaService.getLevel1NameById(pos[0]);
            String area2Name = areaService.getLevel2NameById(pos[1]);
            String area3Name = areaService.getLevel3NameById(pos[2]);
            meter.setPosition(area1Name+area2Name+area3Name);
            meter.setProtocolTypeName(agreementMap.get(meter.getProtocoltype()+""));
            meter.setSubentryTypeName(subentryMap.get(meter.getSubentrytype()+""));
            System.out.print("==============="+meter);
        }
        jsonMain.put("meterList",meterList);
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;
    }
    @RequestMapping("/meterDetail")
    @ResponseBody
    public RestResponse meterDetail(String id, HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        JSONObject jsonMain = new JSONObject();
        Meter meter = meterService.selectMeterById(id);
        List<Agreement> typeList = agreementService.getAgreementType();
        List<Agreement> subentryList = agreementService.getSubentryInfo();
        List<Agreement> typeInfo = agreementService.getAgreementInfo(meter.getProtocoltype());
        String[] areaIds = meter.getPosition().split("-");
        List<Area> area1List = areaService.selectLevel1Area(user.getCompanyid());
        List<Area> area2List = areaService.selectLevel2Area(Integer.parseInt(areaIds[0]));
        List<Area> area3List = areaService.selectLevel3Area(Integer.parseInt(areaIds[1]));
        jsonMain.put("area1List",area1List);
        jsonMain.put("areaIds",areaIds);
        jsonMain.put("area2List",area2List);
        jsonMain.put("area3List",area3List);
        jsonMain.put("typeList",typeList);
        jsonMain.put("subentryList",subentryList);
        jsonMain.put("typeInfo", typeInfo);
        jsonMain.put("meter",meter);
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;
    }
    @RequestMapping("/getAgreement")
    @ResponseBody
    public RestResponse getAgreement( HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        JSONObject jsonMain = new JSONObject();
        List<Area> level1AreaList = areaService.selectLevel1Area(user.getCompanyid());
        List<Agreement> typeList = agreementService.getAgreementType();
        List<Agreement> subentryList = agreementService.getSubentryInfo();
        level1AreaList.sort((o1, o2) -> {
            String str1 = o1.getName();
            String str2 = o2.getName();
            str1 = DateUtil.getNumberStr(str1);
            str2 = DateUtil.getNumberStr(str2);
            return (int) (chineseNumber2Int(str1) - chineseNumber2Int(str2));
        });
        jsonMain.put("level1AreaList",level1AreaList);
        jsonMain.put("typeList",typeList);
        jsonMain.put("subentryList",subentryList);
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;
    }
    @RequestMapping("/insertMeter")
    @ResponseBody
    public RestResponse insertMeter(Meter meter ,HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        meter.setCompanyid(Integer.parseInt(user.getCompanyid()+""));
        if (meterService.selectMeterById(meter.getId())==null){
            meterService.addMeter(meter);
            RestResponse restResponse = RestResponse.succuess();
            return restResponse;
        }else{
            RestResponse restResponse = RestResponse.fail();
            return restResponse;
        }
    }
    @RequestMapping("/delMeter")
    @ResponseBody
    public RestResponse delMeter(String id){
        try {
            meterService.deleteMeter(id);
            RestResponse restResponse = RestResponse.succuess();
            return restResponse;
        }catch (Exception e){
            RestResponse restResponse = RestResponse.fail();
            return restResponse;
        }

    }
    @RequestMapping("/getTypeInfo")
    @ResponseBody
    public RestResponse getTypeInfo(Integer typeId){
        JSONObject jsonMain = new JSONObject();
        if (typeId==null||"".equals(typeId)){

        }else {
            List<Agreement> typeInfo = agreementService.getAgreementInfo(typeId);
            jsonMain.put("typeInfo", typeInfo);
        }
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;
    }
    @RequestMapping("/modifyMeter")
    @ResponseBody
    public RestResponse modifyMeter(Meter meter){
            meterService.updateMeter(meter);
            RestResponse restResponse = RestResponse.succuess();
            return restResponse;
    }
    @RequestMapping("/getArea2")
    @ResponseBody
    public RestResponse getArea2(Integer level1Id){
        JSONObject jsonMain = new JSONObject();
        if (level1Id==null||"".equals(level1Id)){

        }else {
            List<Area> level2AreaList = areaService.selectLevel2Area(level1Id);
            jsonMain.put("level2AreaList", level2AreaList);
        }
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;
    }
    @RequestMapping("/getArea3")
    @ResponseBody
    public RestResponse getArea3(Integer level2Id){
        JSONObject jsonMain = new JSONObject();
        if (level2Id==null||"".equals(level2Id)){

        }else {
            List<Area> level3AreaList = areaService.selectLevel3Area(level2Id);
            jsonMain.put("level3AreaList", level3AreaList);
        }
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;
    }
}
